from pyparsing import Literal, pythonStyleComment, cppStyleComment, LineEnd

from base import Base, GypsumError
from helper import parser
from junos import JunOSPolicy as junospol

class ACLParser(Base):
    def __init__(self, Filename, Config):
        super(ACLParser, self).__init__()
        self._name = Filename
        self._junos = junospol(Config)

    @property
    def grammar(self):
        rule = self._junos.grammar
        rule.ignore(Literal("\\") + LineEnd())
        rule.ignore(pythonStyleComment)
        rule.ignore(cppStyleComment)

        return rule

    def parse(self):
        try:
            results = parser(self._name, self.grammar)
        except GypsumError as err:
            self.error(str(err))

    @property
    def header(self):
        return self._junos.header

    @property
    def pfls(self):
        return self._junos.prefixlist

    @property
    def terms(self):
        return self._junos.value

def parse_config(filename, config):
    conf = ACLParser(filename, config)
    conf.parse()
    return conf
